/***************************************************************************
 *   Copyright (C) 2007 by vasaka,,,   *
 *   gorinich.zmey at gmail   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "cleanclass.h"

#include <math.h>
#include <SDL/SDL.h>
#include "subprojects/SDL_draw/SDL_draw.h"
#include "subprojects/bitmapimage.h"

int width=800, height=600;
char drv_name[16];

int x=0;int y=0;int xo=1;int yo=1; //ugly way to draw a line
int poitselect=0;//0 if no points selected, 1 if one,2 if two
//-----------------------------------------------------------
//-----------------------------------------------------------
void processEvent()
{
  SDL_Event event;
//  r=SDL_PollEvent(&event);
  for ( ;; )
  {
    if ( SDL_WaitEvent ( &event ) )
    {
      if ( event.type == SDL_MOUSEMOTION )
      {
        SDL_ShowCursor ( SDL_ENABLE );
      }       
      if ( event.type == SDL_MOUSEBUTTONUP )
      {
        printf ( "x=%i y=%i\n",event.button.x,event.button.y );
        if ((poitselect==0)||(poitselect==2))
        {
          poitselect=1;
          x=event.button.x;
          y=event.button.y;
        }
        else
        {
          poitselect=2;
          xo=event.button.x;
          yo=event.button.y;          
          break;
        }
      }    
    }
    else
    {
      printf ( "Error while waiting event\n" );
      break;
    }    
  }
}

//copies first channel to second and third
void CopyChannel ( int* img_,int width_,int height_)
{
	for ( int i=0;i<width_*height_; i++ ) 
	{
		img_[i]=((img_[i]&0x000000ff)<<0)+((img_[i]&0x000000ff)<<8)+((img_[i]&0x000000ff)<<16);
	}
}

void MultChannel ( int* img_,int width_,int height_)
{
	for ( int i=0;i<width_*height_; i++ ) 
	{
		img_[i]=(img_[i]<<4);
	}
}

void RotateLine(int &x,int &y,int &xo,int &yo,double alpha)
{
  double xoo = double(x-xo)*cos(alpha)-double(y-yo)*sin(alpha);
  double yoo = double(x-xo)*sin(alpha)+double(y-yo)*cos(alpha);
  xo=int(x-xoo);yo=int(y-yoo);
}
void ShiftLine(int &x,int &y,int &xo,int &yo,double shift)
{
  y=int(y+shift);yo=int(yo+shift);
}
//=========================== Main ==================================================
int main(int argc, char *argv[])
{
  CleanClass CleanerObject_;  
//  setenv("SDL_VIDEODRIVER","aalib",1);
  int r=SDL_Init ( SDL_INIT_VIDEO ); printf ( "SDL_init ret: %i\n",r );
  if ( SDL_VideoDriverName ( drv_name,16 ) ) printf ( "Driver: %s\n",drv_name );

//  SDL_Surface *screen = SDL_SetVideoMode(640,480,32, SDL_OPENGL);
  SDL_Surface *screen = SDL_SetVideoMode ( width,height,32, SDL_SWSURFACE );
//  SDL_Surface *screen = SDL_SetVideoMode(width,height,8, SDL_SWSURFACE);

  SDL_Surface *pictFF = SDL_LoadBMP ( "Input.bmp" ); 
  if ( pictFF ) printf ( "Picture was loaded\n" );
  SDL_Surface *pict = SDL_CreateRGBSurface  (SDL_SRCCOLORKEY,pictFF->w,pictFF->h,32,screen->format->Rmask,screen->format->Gmask,screen->format->Bmask,screen->format->Amask);
  SDL_Surface *outpict = SDL_CreateRGBSurface  (SDL_SRCCOLORKEY,pictFF->w,pictFF->h,32,screen->format->Rmask,screen->format->Gmask,screen->format->Bmask,screen->format->Amask);
  SDL_BlitSurface ( pictFF,NULL,pict,NULL );
  int *scr= ( int* ) screen->pixels; for ( int i=0;i<width*height; i++ ) scr[i]=0x00ffffff;
  
  printf ( "Start load picture bpp %d\n", pict->format->BitsPerPixel );
  CleanerObject_.LoadImage ( ( int* )pict->pixels, pict->w,pict->h);
  printf ( "doProcess\n");
  CleanerObject_.doProcess();
  printf ( "PaintDistribution\n");
  CleanerObject_.PaintDistribution(scr,width,height,0x00000000);
  SDL_Flip ( screen );
  SDL_SaveBMP(screen,"Distr.bmp");
  processEvent();
  printf ( "Clean process img addr %p\n",( int* )pict->pixels);
  CleanerObject_.CleanPaintBack(( int* )outpict->pixels,x,y,xo,yo,width,height);
  //CleanerObject_.CleanPaintBackSimpleBW(( int* )outpict->pixels);
  //CleanerObject_.PaintBackNoClean();

  printf ( "Copy process\n");
  //CopyChannel(( int* )outpict->pixels, pict->w,pict->h);
  printf ( "Save picture bpp %d\n", pict->format->BitsPerPixel );
  SDL_SaveBMP(outpict,"Out.bmp");
 
  SDL_BlitSurface ( outpict,NULL,screen,NULL );
  SDL_Flip ( screen );

  SDL_Event event;
  for ( ;; )
  {
    if ( SDL_WaitEvent ( &event ) )
    {
      if ( event.type == SDL_KEYDOWN )
      {
 //       printf ( "Up,Down - move line\n Left,right - turn line\n Enter - reclean, ESC - exit\n" );
        if ( event.key.keysym.sym == SDLK_ESCAPE ) { printf ( "Break by ESC\n" ); break; }
        
        if ( event.key.keysym.sym == SDLK_UP ) 
        {
          CleanerObject_.PaintDistribution(scr,width,height,0x00ff0000);
          SDL_Flip ( screen );        	
          Draw_Line(screen,x,y,xo,yo,0x00ffffff); 
          ShiftLine(x,y,xo,yo,-1);
          Draw_Line(screen,x,y,xo,yo,0x00ff0000);    
          SDL_Flip ( screen ); 
        }
        if ( event.key.keysym.sym == SDLK_DOWN )
        {
          CleanerObject_.PaintDistribution(scr,width,height,0x00ff0000);
          SDL_Flip ( screen );        	
          Draw_Line(screen,x,y,xo,yo,0x00ffffff); 
          ShiftLine(x,y,xo,yo,1);
          Draw_Line(screen,x,y,xo,yo,0x00ff0000);    
          SDL_Flip ( screen ); 
        }
        
        if ( event.key.keysym.sym == SDLK_LEFT )
        {
          CleanerObject_.PaintDistribution(scr,width,height,0x00ff0000);
          SDL_Flip ( screen );        	
          Draw_Line(screen,x,y,xo,yo,0x00ffffff); 
          RotateLine(x,y,xo,yo,-0.01);
          Draw_Line(screen,x,y,xo,yo,0x00ff0000);    
          SDL_Flip ( screen ); 
        }        	
        if ( event.key.keysym.sym == SDLK_RIGHT )
        {
          CleanerObject_.PaintDistribution(scr,width,height,0x00ff0000);
          SDL_Flip ( screen );        	
          Draw_Line(screen,x,y,xo,yo,0x00ffffff); 
          RotateLine(x,y,xo,yo,0.01);
          Draw_Line(screen,x,y,xo,yo,0x00ff0000);    
          SDL_Flip ( screen ); 
        }              	
        
        if ( event.key.keysym.sym == SDLK_RETURN ) 
        { 
          CleanerObject_.CleanPaintBackBW(x,y,xo,yo,width,height); 
          CopyChannel(( int* )pict->pixels, pict->w,pict->h);
          SDL_SaveBMP(pict,"Out.bmp");
         
          SDL_BlitSurface ( pict,NULL,screen,NULL );
          SDL_Flip ( screen );           
        }
        
        if ( event.key.keysym.sym == SDLK_w ) 
        { 
          CleanerObject_.PaintDistribution(scr,width,height,0x00ff0000);
          SDL_Flip ( screen );
          processEvent(); 
          CleanerObject_.CleanPaintBackBW(x,y,xo,yo,width,height);
          printf ( "Copy process\n");
          CopyChannel(( int* )pict->pixels, pict->w,pict->h);
          printf ( "Save picture bpp %d\n", pict->format->BitsPerPixel );
          SDL_SaveBMP(pict,"Out.bmp");
         
          SDL_BlitSurface ( pict,NULL,screen,NULL );
          SDL_Flip ( screen );          
        }
      }
      if ( event.type == SDL_MOUSEMOTION )
      {
        SDL_ShowCursor ( SDL_ENABLE );
      }   
    }
    else
    {
      printf ( "Error while waiting event\n" );
      break;
    }
  }
  
  SDL_Quit();
  return 0;
}
//===================================================================================
